/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.io.IOException;
import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.el.VariableResolver;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import org.apache.commons.collections.Factory;

/**
 *
 * @author juampi
 */
public class LinkFilter implements Filter {

    

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        /*
        FacesContext context = getFacesContext(request, response);
        UserBean bean = (UserBean)getBean(context, "userBean");
        bean.borrarTodo();
        */
        chain.doFilter(request, response);
    }

    public void destroy() {
        
    }
    public static void main(String arg[]) {
        String direccion="pagina/prueba.jsp";
        String arre[]=direccion.split("/");
        for (int i = 0; i < arre.length; i++) {
            System.out.println(arre[i]);
        }
    }
    private Object getBean(FacesContext context,String name) {
        return context.getApplication().getVariableResolver().resolveVariable(context, name);
    }
    private FacesContext getFacesContext(final ServletRequest req,final ServletResponse res) {
        FacesContext r;
        FacesContext context = FacesContext.getCurrentInstance();
        if(context!=null) {
            r = context;
        } else {
            FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
            LifecycleFactory lifeFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
            Lifecycle lifecycle = lifeFactory.getLifecycle(lifeFactory.DEFAULT_LIFECYCLE);
            ServletContext servletContext = ((HttpServletRequest) req).getSession().getServletContext();
            context = contextFactory.getFacesContext(servletContext, req, res, lifecycle);
            r = context;
        }
        return r;
    }
}
